package com.tn.dao;

import java.util.ArrayList;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.Query;
import com.tn.bo.Company;
import com.tn.bo.UserLoginDetails;
import com.tn.bo.UserProfile;
import com.tn.bo.UserRegMapping;
import com.tn.service.OfyDataService;

public class UserDAO {
	public UserLoginDetails findUserByUserId(String userId){
		Objectify ofy = OfyDataService.getInstance().getOfyService();
		Query<UserLoginDetails> query = ofy.query(UserLoginDetails.class).filter("userId", userId);
		
		for(UserLoginDetails details: query){
			return details;
		}
		
		return null;
	}
	
	public UserProfile getUserProfile(Long userProfileId){
		Objectify ofy = OfyDataService.getInstance().getOfyService();
		return ofy.get(new Key<UserProfile>(UserProfile.class,userProfileId));
	}
	
	
	public long registerUser(UserProfile profile, UserLoginDetails loginDetails){
		Objectify txn = OfyDataService.getInstance().beginOfyTransaction();
		Key<UserProfile> userProfileKey = txn.put(profile);
		OfyDataService.getInstance().commitOfyTransaction(txn);
		loginDetails.setUserProfileId(profile.getUserProfileId());
		Objectify txn2 = OfyDataService.getInstance().beginOfyTransaction();
		txn2.put(loginDetails);
		OfyDataService.getInstance().commitOfyTransaction(txn2);
		return userProfileKey.getId();
		//return true;
	}
	
	public boolean updateUserLoginDetails(UserLoginDetails loginDetails){
		Objectify txn2 = OfyDataService.getInstance().beginOfyTransaction();
		txn2.put(loginDetails);
		OfyDataService.getInstance().commitOfyTransaction(txn2);
		return true;
	}
	
	public void saveUserRegMapping(UserRegMapping regMapping){
		Objectify txn = OfyDataService.getInstance().beginOfyTransaction();
		txn.put(regMapping);
		OfyDataService.getInstance().commitOfyTransaction(txn);
	}
	
	public String findUserRegMapping(String regHash){
		String userId = null;
		Objectify ofy = OfyDataService.getInstance().getOfyService();
		Query<UserRegMapping> q = ofy.query(UserRegMapping.class);
		q.filter("regHash", regHash);
		q.filter("status", UserRegMapping.STATUS_UNUSED);
		UserRegMapping mapping = q.get();
		if(mapping != null){
			userId = mapping.getUserId();
		}
		
		return userId;
	}
	
	public void updateUser(UserProfile profile){
		Objectify txn = OfyDataService.getInstance().beginOfyTransaction();
		Key<UserProfile> userProfileKey = txn.put(profile);
		OfyDataService.getInstance().commitOfyTransaction(txn);
	}

	public ArrayList<UserProfile> getByProperty(String propName, String propValue) {
		
			Objectify ofy = OfyDataService.getInstance().getOfyService();
			Query<UserProfile> q = ofy.query(UserProfile.class);
			q.filter(propName, propValue);
			ArrayList<UserProfile> retList = new ArrayList<UserProfile>();
			for(UserProfile userProfile: q){
				retList .add(userProfile);
			}

			return retList;
		
	
	}
	
	public void listAllUsers(){
		Objectify ofy = OfyDataService.getInstance().getOfyService();
		Query<UserLoginDetails> query = ofy.query(UserLoginDetails.class);
		CompanyDAO cd = new CompanyDAO();
		for(UserLoginDetails details: query){
			System.out.println("LOGIN ID: " + details.getUserId());
			UserProfile profile = getUserProfile(details.getUserProfileId());
			try{
			Company c = cd.findCompanyByCompanyId(profile.getCompanyId());
			System.out.println("COMPANY: " + c.getCompanyName());
			}catch(Exception ex){
				System.out.println("COMPANY: NOT FOUND.");
			}
			System.out.println("*******************");
		}
	}
}
